//
//  XJHomeViewController.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/2.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJHomeViewController.h"
//controller
//#import "XJSearchViewController.h"
#import "XJZPQSearchWYViewController.h" //7-5搜索

#import "XJLocaltionViewController.h" //定位
#import "XJSeckillGoodsViewController.h"
#import "XJRecommendGoodsViewController.h"
#import "XJGoodsDetailViewController.h"
#import "XJCommunityViewController.h" // 5-30 社区
//#import "XJLocalTravelViewController.h" // 6-4本地旅游
#import "XJLocalTravelLoadDataVC.h"
#import "XJShopListViewController.h" //6-15 宅配圈(店铺列表)
#import "RBGoodsDetailViewController.h" // 商品详情
#import "XJTagSuperMarketViewController.h" // 6-25超市商场
#import "XJWaiBuAppViewController.h" //7-8 外部app
#import "XJWebViewController.h" //7-8 如果是url是网页点击轮播图直接跳网页

//cell
#import "XJHomeTagCollectionViewCell.h"//标签
#import "XJSeckillGoodsCollectionViewCell.h"//秒杀
#import "XJRecommendGoodsCollectionViewCell.h"//推荐
//header
#import "XJHomeHeaderView.h"
#import "XJSeckillGoodsHeader.h"
#import "XJRecommendGoodsHeader.h"
#import "XJHomeRefreshHeader.h"
#import "XJHomeTagSectionHeader.h" // 6-22 tag的区头 h：200

//footer
#import "XJGoodsOverFooter.h"
//tools
#import "XJRequestHelper.h"
#import <UIImageView+WebCache.h>
#import "XJNet.h"
//model
#import "HomeModel.h"
#import "BannerListModel.h"
#import "HomeTagsModel.h"
#import "SeckillGoodsModel.h"
#import "RecommendGoodsModel.h"

#import "XJLocationTool.h"

@interface XJHomeViewController ()<UITextFieldDelegate,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,XJSeckillGoodsHeaderDelegate,XJRecommendGoodsHeaderDelegate>

@property (nonatomic, weak) UITextField * searchText;
@property (nonatomic, strong) UIImageView * localImgV;
@property (nonatomic, strong) UIButton * adderssBtn;

/* collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;

//banner图
@property (nonatomic, strong) NSMutableArray * bannerArray;;
//标签
@property (nonatomic, strong) NSMutableArray * homeTagsArray;
//秒杀商品
@property (nonatomic, strong) NSMutableArray * seckillGoodsArray;
//推荐商品
@property (nonatomic, strong) NSMutableArray * recommendGoodsArray;

/**6-23记录当前定位的model 注意属性修饰符 如果用copy 直接炸❌*/
@property (nonatomic, strong) BMKPoiInfo *currentPoiInfo;


@end

//cell
static NSString * const HomeTagsCellID = @"XJHomeTagCollectionViewCell";
static NSString * const SeckillCellID = @"XJSeckillGoodsCollectionViewCell";
static NSString * const RecommendCellID = @"XJRecommendGoodsCollectionViewCell";
//header
static NSString *const HomeHeaderID = @"XJHomeHeaderView";
static NSString *const SeckillHeaderID = @"XJSeckillGoodsHeader";
static NSString *const RecommendHeaderID = @"XJRecommendGoodsHeader";
static NSString *const HomeTagSectionHeaderId = @"XJHomeTagSectionHeaderId";

//footer
static NSString *const OverFooterID = @"XJGoodsOverFooter";

@implementation XJHomeViewController

#pragma mark 懒加载
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        //注册
        //cell
        [_collectionView registerClass:[XJHomeTagCollectionViewCell class] forCellWithReuseIdentifier:HomeTagsCellID];
        [_collectionView registerClass:[XJSeckillGoodsCollectionViewCell class] forCellWithReuseIdentifier:SeckillCellID];
        [_collectionView registerClass:[XJRecommendGoodsCollectionViewCell class] forCellWithReuseIdentifier:RecommendCellID];
         //header
        [_collectionView registerClass:[XJHomeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeHeaderID];
        [_collectionView registerClass:[XJSeckillGoodsHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SeckillHeaderID];
        [_collectionView registerClass:[XJRecommendGoodsHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:RecommendHeaderID];
        [_collectionView registerClass:[XJHomeTagSectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTagSectionHeaderId]; //tag的区头

        
        
        //footer
        [_collectionView registerClass:[XJGoodsOverFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:OverFooterID];

        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UITextField *)searchText{
    
    if (!_searchText) {
        UITextField *searchText = [[UITextField alloc]initWithFrame:CGRectMake(ADAPTWidth(131),UI_IS_IPHONE5?ADAPTHeight(14):ADAPTHeight(9), ADAPTWidth(200), ADAPTHeight(24))];
        [self.navigationController.navigationBar addSubview:searchText];
        self.searchText=searchText;
    }
    return _searchText;
}


- (UIImageView *)localImgV{
    if (!_localImgV) {
        UIImageView * localImgV = [[UIImageView alloc] initWithFrame:CGRectMake(ADAPTWidth(12), UI_IS_IPHONE5?ADAPTHeight(18):ADAPTHeight(13), ADAPTWidth(12), ADAPTHeight(14))];
        self.localImgV = localImgV;
        [self.navigationController.navigationBar addSubview:localImgV];
    }
    return _localImgV;
}

- (UIButton *)adderssBtn{
    
    if (!_adderssBtn) {
        UIButton * adderssBtn = [UIButton new];
        self.adderssBtn = adderssBtn;
        [self.navigationController.navigationBar addSubview:adderssBtn];
    }
    return _adderssBtn;
}

- (NSMutableArray *)bannerArray{
    if (!_bannerArray) {
        NSMutableArray * bannerArray = [[NSMutableArray alloc]init];
        self.bannerArray = bannerArray;
    }
    return _bannerArray;
}

- (NSMutableArray *)homeTagsArray{
    if (!_homeTagsArray) {
        NSMutableArray * homeTagsArray = [[NSMutableArray alloc]init];
        self.homeTagsArray = homeTagsArray;
    }
    return _homeTagsArray;
}

- (NSMutableArray *)seckillGoodsArray{
    if (!_seckillGoodsArray) {
        NSMutableArray * seckillGoodsArray = [[NSMutableArray alloc]init];
        self.seckillGoodsArray = seckillGoodsArray;
    }
    return _seckillGoodsArray;
}

- (NSMutableArray *)recommendGoodsArray{
    if (!_recommendGoodsArray) {
        NSMutableArray * recommendGoodsArray = [[NSMutableArray alloc]init];
        self.recommendGoodsArray = recommendGoodsArray;
    }
    return _recommendGoodsArray;
}

#pragma mark 子视图
- (void)initSubviews{
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.frame = CGRectMake(0, 0, KWidth, KHeight - XJBottomTabH);
//    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = WHITE_COLOR;

}

#pragma mark 搜索
- (void)initSearchText{
    self.searchText.placeholder=@"搜索的说明";
    self.searchText.layer.masksToBounds=YES;
    self.searchText.layer.cornerRadius=ADAPTHeight(24)/2;
    self.searchText.backgroundColor=XJCOLOR_RGB(246.0, 247.0, 249.0);
    [self.searchText setValue:XJCOLOR_RGB(157.0, 165.0, 168.0) forKeyPath:@"_placeholderLabel.textColor"];
    self.searchText.font=XJFONTSET_R(13);
    self.searchText.delegate=self;
    UIView *centerView = [[UIView alloc]init];
    centerView.backgroundColor = [UIColor clearColor];
    centerView.frame = CGRectMake(0, 0, ADAPTWidth(24), ADAPTHeight(24));
    UIImageView * searchImgV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nav_search"]];
    searchImgV.frame = CGRectMake(ADAPTWidth(4), ADAPTHeight(4), ADAPTWidth(16), ADAPTHeight(16));
    [centerView addSubview:searchImgV];
    self.searchText.leftView=centerView;
    self.searchText.leftViewMode=UITextFieldViewModeAlways;
    self.searchText.leftView.contentMode=UIViewContentModeScaleAspectFit;
}

#pragma mark NavgationBar
- (void)initNavgationBar{
    [self.localImgV setImage:[UIImage imageNamed:@"address_list_off"]];
    
    [self.adderssBtn setFrame:CGRectMake(CGRectGetMaxX(self.localImgV.frame)+2.5, UI_IS_IPHONE5?ADAPTHeight(15):ADAPTHeight(10),UI_IS_IPHONE5?ADAPTWidth(100):ADAPTWidth(90), ADAPTHeight(20))];
    //哈哈哈哈
    [self.adderssBtn buttonWithTitle:@"华盛达广场" titleFont:XJFONTSET_R(14) titleColor:XJCOLOR_RGB(17,17,17) imageType:ImageTypeImg  imageName:@"direction_down_black" withTarget:self action:@selector(adderssBtnClick:) style:ImgTextBtnStyleRight margin:5];
    
}

- (void)adderssBtnClick:(UIButton *)btn{
    
    XJLog(@"点击定位");
    XJLocaltionViewController * xjLocalVC = [[XJLocaltionViewController alloc]init];
    
    xjLocalVC.locationViewControllerBlock = ^(BMKPoiInfo *poiInfoM) {
        [self.adderssBtn setTitle:poiInfoM.name forState:(UIControlStateNormal)];
        XJLog(@"%@--%f--%f",poiInfoM.name,poiInfoM.pt.latitude,poiInfoM.pt.longitude);
        
        self.currentPoiInfo = poiInfoM;
    };
    [self.navigationController pushViewController:xjLocalVC animated:YES];
}

#pragma mark - 获取数据

/*
//- (void)getHomeData{
//    XJWeakSelf(self);
//    [[XJRequestHelper shareHelper] setBlockWithReturnBlock:^(id returnValue) {
//
//        HomeModel * model  = [HomeModel mj_objectWithKeyValues:returnValue];
//        for (BannerListModel * blModel in model.banner_list) {
//            [weakself.bannerArray addObject:blModel.image];
//        }
//        XJLog(@"bannerArray => %@",self.bannerArray);
//
//        for (HomeTagsModel * htModel in model.home_tags) {
//            [weakself.homeTagsArray addObject:htModel];
//        }
//        XJLog(@"homeTagsArray => %@",weakself.homeTagsArray);
//        [weakself.collectionView reloadData];
//
//    } failureBlock:^{
//        XJLog(@"请求失败");
//    } errorBlock:^(id errorCode) {
//        XJLog(@"errorCode = %@",errorCode);
//    }];
//
//    [[XJNet loadNet] netWorkReachabilityState:^(BOOL netState) {
//        if (netState) {
//            [[XJRequestHelper shareHelper] getHomeDataWithLatitude:[NSString stringWithFormat:@"37.785834"] longitude:[NSString stringWithFormat:@"122.406417"]];
//        }else{
//            XJLog(@"断开网络连接");
//            [MBProgressHUD showError:@"断开网络连接"];
//        }
//    }];
//}
*/

// 获取首页数据
- (void)requestHomeData {
    //传入坐标获取数据
    
    
    NSString *lat = @"37.785834";
    NSString *lng = @"122.406417";
    NSString *customer_id = [XJUserModel xj_customId]; //该字段可能为空,为空就不传呗
    NSDictionary *parDic = NSDictionaryOfVariableBindings(lat,lng);
    
    if (![NSString isEmptyString:customer_id]) {
        parDic = NSDictionaryOfVariableBindings(lat,lng,customer_id);
    }
    
    [XJHttpTool requestHomeDataWithCoordinate:parDic SuccessHandler:^(id responseObjc) {
        NSDictionary *dataDic = responseObjc[@"data"];
        
        // 字典转模型{key1:数组[模型1],key2:数组[模型2]}
        HomeModel *homeM = [HomeModel mj_objectWithKeyValues:dataDic];
        
        // 给数据源赋值
        self.bannerArray = homeM.banner_list;
        self.homeTagsArray = homeM.home_tags;
        self.seckillGoodsArray = homeM.seckill_goods;
        self.recommendGoodsArray = homeM.recommend_goods;
        
        // 回到主线程中刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } failureHandler:^(NSError *error) {
        NSLog(@"失败:%@",error);

    }];
}
#pragma mark - 设置头部header
- (void)setupGIFRrfresh{
    self.collectionView.mj_header = [XJHomeRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(setUpRecData)];
}

#pragma mark - 刷新
- (void)setUpRecData{
    XJWeakSelf(self);
   // [DCSpeedy dc_callFeedback]; //触动
    [self requestHomeData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //手动延迟
        [weakself.collectionView.mj_header endRefreshing];
    });
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navTitle = @"首页";
    self.navButtonRight.hidden = NO;
    
    //6-20调用定位
    [[XJLocationTool shareLocationTool] startLocation];
    [XJLocationTool shareLocationTool].locationToolBlock = ^(BMKPoiInfo *poiInfo) {
        XJLog(@"地址:%@--%f--%f",poiInfo.name,poiInfo.pt.latitude,poiInfo.pt.longitude);
        // 刷新导航栏左侧定位地址文字
        [self.adderssBtn setTitle:poiInfo.name forState:(UIControlStateNormal)];
        
        self.currentPoiInfo = poiInfo; //第一次标记定位的地址
    };
    

    {
    //隐藏黑线
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent  = NO;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    }
    
    [self initSearchText];
    
    [self initNavgationBar];
    
    [self initSubviews];
    
//    [self getHomeData];
    [self requestHomeData];
    
    [self setupGIFRrfresh];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    return 3;
      return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    if (section == 0) { //标签
//        return self.homeTagsArray.count;
//    }else if (section ==1){
//        return self.seckillGoodsArray.count;//秒杀
//    }else if (section ==2){
//        return self.recommendGoodsArray.count;//推荐
//    }else{
//        return 0;
//    }
    if (section == 2) {
        return self.seckillGoodsArray.count;//秒杀
    }else if (section ==3){
        return self.recommendGoodsArray.count;//推荐
    }else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *xjCell = nil;
    if (indexPath.section == 2){//秒杀
        XJSeckillGoodsCollectionViewCell * xjsgCell = [collectionView dequeueReusableCellWithReuseIdentifier:SeckillCellID forIndexPath:indexPath];

        // 给cell赋值

        xjCell = xjsgCell;
    }else if (indexPath.section == 3){//推荐
        XJRecommendGoodsCollectionViewCell * xjrgCell = [collectionView dequeueReusableCellWithReuseIdentifier:RecommendCellID forIndexPath:indexPath];
        // 给cell赋值
        xjrgCell.rgModel = self.recommendGoodsArray[indexPath.row];
        xjCell = xjrgCell;
    }
    return xjCell;
}

//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    UICollectionViewCell *xjCell = nil;
//    if (indexPath.section == 0) {//标签
//        XJHomeTagCollectionViewCell * xjhgCell = [collectionView dequeueReusableCellWithReuseIdentifier:HomeTagsCellID forIndexPath:indexPath];
//        xjhgCell.htModel = self.homeTagsArray[indexPath.row];
//        xjCell = xjhgCell;
//    }else if (indexPath.section == 1){//秒杀
//        XJSeckillGoodsCollectionViewCell * xjsgCell = [collectionView dequeueReusableCellWithReuseIdentifier:SeckillCellID forIndexPath:indexPath];
//
//        // 给cell赋值
//
//        xjCell = xjsgCell;
//    }else if (indexPath.section == 2){//推荐
//        XJRecommendGoodsCollectionViewCell * xjrgCell = [collectionView dequeueReusableCellWithReuseIdentifier:RecommendCellID forIndexPath:indexPath];
//        // 给cell赋值
//        xjrgCell.rgModel = self.recommendGoodsArray[indexPath.row];
//        xjCell = xjrgCell;
//    }
//    return xjCell;
//}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    
    if (kind == UICollectionElementKindSectionHeader){
        if (indexPath.section == 0) {
            XJHomeHeaderView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeHeaderID forIndexPath:indexPath];
            headerView.bannerArray = [self.bannerArray mutableCopy];
            
            
            XJWeakSelf(self);
            //处理轮播图点击事件
            headerView.didSelectImgBlock = ^(BannerListModel *model) {
                XJLog(@"点击轮播图:%@--%@",model.name,model.bannerId);
                [weakself businessLogicOfLunBoTuWithBannerListModel:model];
            };
            
            return headerView;
        }else if (indexPath.section == 1){ //tag 区头
            
           XJHomeTagSectionHeader *tagHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeTagSectionHeaderId forIndexPath:indexPath];
            tagHeaderView.dataArray = [self.homeTagsArray mutableCopy];
            
            XJWeakSelf(self);
            tagHeaderView.homeTagSectionHeaderBlock = ^(HomeTagsModel *clickedTagModel) {
              
                [weakself bussinessLogicOfHomeTagWithModel:clickedTagModel];
            };
            return tagHeaderView;
        }else if (indexPath.section == 2){
            XJSeckillGoodsHeader *sgHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SeckillHeaderID forIndexPath:indexPath];
            sgHeader.delegate = self;
            return sgHeader;
        }else if (indexPath.section == 3){
            XJRecommendGoodsHeader *rgHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:RecommendHeaderID forIndexPath:indexPath];
            rgHeader.delegate = self;
            return rgHeader;
        }else{
            return nil;
        }
    }else if (kind == UICollectionElementKindSectionFooter){
        if (indexPath.section==3) {
            XJGoodsOverFooter * overFooter = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:OverFooterID forIndexPath:indexPath];
            return overFooter;
        }else{
            return nil;
        }
    }else{
        return nil;
    }
}

#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
    }else if (indexPath.section == 3) {
        return CGSizeMake((KWidth - 4)/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
    }else {
        return CGSizeZero;
    }
    
//    if (indexPath.section == 0) {//9宫格组
//        return CGSizeMake(KWidth/5 , KWidth/5 + XJMargin);
//    }else if (indexPath.section == 1){
//        return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
//    }
//    else if (indexPath.section == 2){
//        return CGSizeMake((KWidth - 4)/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
//    }else{
//        return CGSizeZero;
//    }
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            layoutAttributes.size = CGSizeMake(KWidth, KWidth * 0.38);
        }else if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 4){
            layoutAttributes.size = CGSizeMake(KWidth * 0.5, KWidth * 0.24);
        }else{
            layoutAttributes.size = CGSizeMake(KWidth * 0.25, KWidth * 0.35);
        }
    }
    return layoutAttributes;
}


#pragma mark - head宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
//    if (section == 0) {
//        return CGSizeMake(KWidth, ADAPTHeight(180)); //图片滚动的宽高
//    }else if (section == 1){
//        return CGSizeMake(KWidth, UI_IS_IPHONE5 ? ADAPTHeight(80) : ADAPTHeight(70));
//    }else if (section == 2){
//        return CGSizeMake(KWidth, ADAPTHeight(60));
//    }else{
//        return CGSizeZero;
//    }
    
    if (section == 0) {
        return CGSizeMake(KWidth, ADAPTHeight(180)); //图片滚动的宽高
    }else if (section == 1){
        return CGSizeMake(KWidth, 200);
//        return CGSizeMake(KWidth, UI_IS_IPHONE5 ? ADAPTHeight(80) : ADAPTHeight(70));
    }else if (section == 2){
        return CGSizeMake(KWidth, ADAPTHeight(60));
    }else if (section == 3) {
        return CGSizeMake(KWidth, ADAPTHeight(60));
    }
    else{
        return CGSizeZero;
    }
}

#pragma mark - foot宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 3) { //2直接炸了
        return CGSizeMake(KWidth, ADAPTHeight(60));  //Top头条的宽高
    }else{
        return CGSizeZero;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 1 || section == 2) {
        return 1;
    }else{
        return 0;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
   if (indexPath.section == 2){
       
        XJLog(@"点击了秒杀的第%zd个商品",indexPath.row + 1);
        NSString *goodIdStr = [self.seckillGoodsArray[indexPath.row] good_id];
        [XJGoodsDetailViewController goodsDetailWithController:self goodsType:GoodsDetailTypeSecKill goodsId:goodIdStr];
    }else if (indexPath.section == 3){
        XJLog(@"点击了推荐的第%zd个商品",indexPath.row + 1);
        NSString *recommendGoodId = [self.recommendGoodsArray[indexPath.row] good_id];
        
        RBGoodsDetailViewController *detailVC = [[RBGoodsDetailViewController alloc] init];
        detailVC.goodsId = recommendGoodId;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (indexPath.section == 0) {//10
//        XJLog(@"点击了第%zd个标签",indexPath.row + 1);
//
//        [self clickTagWithRow:indexPath.row];
//
//    }else if (indexPath.section == 1){
//        XJLog(@"点击了秒杀的第%zd个商品",indexPath.row + 1);
////        [XJGoodsDetailViewController initWithController:self goodsType:GoodsDetailTypeSecKill];
//
//        NSString *goodIdStr = [self.seckillGoodsArray[indexPath.row] good_id];
//
//        [XJGoodsDetailViewController goodsDetailWithController:self goodsType:GoodsDetailTypeSecKill goodsId:goodIdStr];
//
//    }else if (indexPath.section == 2){
//        XJLog(@"点击了推荐的第%zd个商品",indexPath.row + 1);
//        NSString *recommendGoodId = [self.recommendGoodsArray[indexPath.row] good_id];
//
//        RBGoodsDetailViewController *detailVC = [[RBGoodsDetailViewController alloc] init];
//        detailVC.goodsId = recommendGoodId;
//        [self.navigationController pushViewController:detailVC animated:YES];
//
////        [XJGoodsDetailViewController goodsDetailWithController:self goodsType:GoodsDetailTypeRecommend goodsId:recommendGoodId];
//    }
//}



#pragma mark UITextFiledDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//    XJSearchViewController * xjSearchVC = [[XJSearchViewController alloc]init];
    
    XJZPQSearchWYViewController * xjSearchVC = [XJZPQSearchWYViewController suspendXJZPQSearchVYView];
    [self.navigationController pushViewController:xjSearchVC animated:YES];
    return NO;
}

#pragma mark XJSeckillGoodsHeaderDelegate

- (void)xjsg_moreBtnAction{
    XJSeckillGoodsViewController * xjsgVC = [[XJSeckillGoodsViewController alloc]init];
    
    xjsgVC.lat = [self rb_latitude];
    xjsgVC.lng = [self rb_longitude];
    
    [self.navigationController pushViewController:xjsgVC animated:YES];
    
}

#pragma mark XJRecommendGoodsHeaderDelegate

- (void)xjrg_moreBtnAction{
    
    XJRecommendGoodsViewController * xjrgVC = [[XJRecommendGoodsViewController alloc]init];
    [self.navigationController pushViewController:xjrgVC animated:YES];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //self.tabBarController.tabBar.hidden = NO;
    
    [self.adderssBtn setHidden:NO];
    [self.localImgV setHidden:NO];
    [self.searchText setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.adderssBtn setHidden:YES];
    [self.localImgV setHidden:YES];
    [self.searchText setHidden:YES];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 5-30 banner 圆形图标 点击操作
/**点击banner 下面的标签(tag)*/
- (void)bussinessLogicOfHomeTagWithModel:(HomeTagsModel *)model {
    [self gotoZaiPeiQuanVCWithHomeTagModel:model];
    [self gotoCommunityVCWithHomeTagModel:model];
    [self gotoLocalTravelWithHomeTagModel:model];
    [self gotoWaiBuAppWithHomeTagModel:model];
    [self gotoChargeWithHomeTagModel:model];
    [self gotoSupermarketHomeTagModel:model];
}

//跳转“宅配圈”
- (void)gotoZaiPeiQuanVCWithHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"宅配圈"]) {
        
        [self gotoZaiPeiQuanVC];
    }
}
- (void)gotoZaiPeiQuanVC {
    XJShopListViewController *shopListVC = [[XJShopListViewController alloc] init];
    // 传递经纬度。
    shopListVC.lng = [self rb_longitude];
    shopListVC.lat = [self rb_latitude];
    
    [self.navigationController pushViewController:shopListVC animated:YES];
}


// 跳转'社区在线'页面
- (void)gotoCommunityVCWithHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"社区在线"]) {
        XJLog(@"点击的:%@ tag",tagModel.name);
        // 将经纬度传递过去
//        NSString *lng  = @"120.189459";
//        NSString *lat = @"30.194977";
        
        [self gotoCommunityVC];
    }
}
- (void)gotoCommunityVC {
    XJCommunityViewController *communityVC = [[XJCommunityViewController alloc] init];
    communityVC.lng = [self rb_longitude];
    communityVC.lat = [self rb_latitude];
    [self.navigationController pushViewController:communityVC animated:YES];
}
/**跳转本地旅游*/
- (void)gotoLocalTravelWithHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"本地旅游"]) {
        
        XJLocalTravelLoadDataVC *localVC = [XJLocalTravelLoadDataVC new];
        localVC.localCity = @"杭州";
        
        [self.navigationController pushViewController:localVC animated:YES];
    }
}
- (void)gotoWaiBuAppWithHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"外部ap"]) {
//        [JRToast showWithText:tagModel.name];
        
        [self.navigationController pushViewController:[XJWaiBuAppViewController new] animated:YES];
    }
}
//跳转缴费充值
- (void)gotoChargeWithHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"缴费充值"]) {
        [JRToast showWithText:tagModel.name];
  
    }
}
//跳转超市商场
- (void)gotoSupermarketHomeTagModel:(HomeTagsModel *)tagModel {
    if ([tagModel.name isEqualToString:@"超市商场"]) {
//        [JRToast showWithText:tagModel.name];
        
        
        XJLog(@"请求参数是:%@",tagModel.params); //{"goods_category_id":105}
        
        XJTagSuperMarketViewController *supermarketVC = [[XJTagSuperMarketViewController alloc] init];
        
        //json---》NSDic
        NSDictionary *tempDic = [self rb_dicFromJsonString:tagModel.params];
        supermarketVC.goods_category_id = [NSString stringWithFormat:@"%@",tempDic[@"goods_category_id"]];
        
        supermarketVC.lat = [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.latitude];
        supermarketVC.lat = [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.latitude];

        supermarketVC.lng = [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.longitude];
        
        [self.navigationController pushViewController:supermarketVC animated:YES];
    }
}

- (void)gotoSuperMarketVCWithJSonStr:(NSString *)goods_category_id {
    XJTagSuperMarketViewController *supermarketVC = [[XJTagSuperMarketViewController alloc] init];
    
    supermarketVC.goods_category_id = goods_category_id;
    
    supermarketVC.lat = [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.latitude];
    supermarketVC.lng = [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.longitude];
    
    [self.navigationController pushViewController:supermarketVC animated:YES];
}


- (NSDictionary *)rb_dicFromJsonString:(NSString *)jsonstr {
    if (jsonstr == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonstr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *err = nil;
    NSDictionary *dic =   [NSJSONSerialization JSONObjectWithData:jsonData options:(NSJSONReadingMutableContainers) error:&err];
    
    if (err) {
        XJLog(@"解析失败:%@",err);
        return nil;
    }
    return dic;
}



#pragma mark - 处理轮播图点击事件逻辑
- (void)businessLogicOfLunBoTuWithBannerListModel:(BannerListModel *)model {
    
    XJLog(@"%@-%@",model.bannerId,model.url);
    
    if ([model.bannerId isEqualToNumber:@56]) { //测试1 ✅
        
        XJWebViewController *webVC = [XJWebViewController new];
        webVC.url = model.url;
        
    }else if([model.bannerId isEqualToNumber:@55]) { //用户 ✅
        
        NSString *str = [self separateStringTwiceWithStr:model.url];
        if([NSString isEmptyString:str]) return;
        
        [self gotoSuperMarketVCWithJSonStr:str];
        
    }else if([model.bannerId isEqualToNumber:@54]) { //店铺 ✅
        [self gotoZaiPeiQuanVC];
        
    }else if([model.bannerId isEqualToNumber:@53]) { //社区 ✅
        [self gotoCommunityVC];
    }else if ([model.bannerId isEqualToNumber:@48]){ //开业大降价
        
        NSString *str = [self separateStringTwiceWithStr:model.url];
        if([NSString isEmptyString:str]) return;
        [self gotoSuperMarketVCWithJSonStr:str];
        
    }else if([model.bannerId isEqualToNumber:@46]){ //超市大减价。 ✅
        NSString *str = [self separateStringTwiceWithStr:model.url];
        if([NSString isEmptyString:str]) return;
        [self gotoSuperMarketVCWithJSonStr:str];

    }else {
        XJLog(@"点击首页轮播图");
    }
}


// categorylist.html?goods_category_id=96 ===> 96
- (NSString *)separateStringTwiceWithStr:(NSString *)str{
    NSArray *sepArr = [str componentsSeparatedByString:@"?"];
    XJLog(@"%@",[sepArr lastObject]);
    NSString *lastStr = [sepArr lastObject];
    NSString *lastStr2 =  [[lastStr componentsSeparatedByString:@"="] lastObject];
    
    return lastStr2;
}

#pragma mark - 6-23 private 经纬度
- (NSString *)rb_longitude {
    //    NSDictionary *zuoBiao = @{@"lat":@"30.19489",@"lng":@"120.189604"};
    
    XJLog(@"%d",(int)self.currentPoiInfo.pt.longitude);
    
    //至少保证不为空
//    NSString *lng =  [NSString stringWithFormat:@"%f",self.currentPoiInfo.pt.longitude]; //0.000000
    NSString *lng =  [NSString stringWithFormat:@"%d",(int)self.currentPoiInfo.pt.longitude]; //0.000000

    if ([NSString isEmptyString:lng] ||  [lng isEqualToString:@"0"]) {
        return @"120.189604";
    }else {
        return lng;
    }
}
- (NSString *)rb_latitude {
    
    NSString *lat =  [NSString stringWithFormat:@"%d",(int)self.currentPoiInfo.pt.latitude];
    
    if ([NSString isEmptyString:lat] || [lat isEqualToString:@"0"]) {
        return @"30.19489";
    }else {
        return lat;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
